package com.hpin.mlxteacher.child;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.ChildDetailResult;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
//import com.umeng.analytics.MobclickAgent;

/**
 * 孩子家庭详情页面
 * Created by Aaron on 15/7/29.
 */
public class ChildDetailActivity extends BaseActivity {

    //孩子的id
    private String id;
    private TextView tv_child;
    private TextView tv_nature;
    private TextView tv_birh;
    private TextView tv_pleacecode;
    private TextView tv_dad;
    private TextView tv_job1;
    private TextView tv_tel;
    private TextView tv_mom;
    private TextView tv_job2;
    private TextView tv_phone;
    private TextView tv_roomdetail;
    private TextView tv_local;
    private TextView tv_sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_child_detail);

        id = getIntent().getStringExtra("id");

        initWidget();

        getData();

    }

    //    CommonUtils.isNull(childDetailResult.token)?"":
    private void setData(ChildDetailResult childDetailResult) {

        if (childDetailResult.data != null) {

            tv_child.setText(childDetailResult.data.name);
            tv_sex.setText(childDetailResult.data.sex);
            tv_birh.setText(childDetailResult.data.birthDay);
            tv_pleacecode.setText(childDetailResult.data.inviteKey);
            tv_dad.setText(childDetailResult.data.fatherName);
            tv_job1.setText(childDetailResult.data.faWorkUnit);
            tv_tel.setText(childDetailResult.data.faTelephone);
            tv_mom.setText(childDetailResult.data.motherName);
            tv_job2.setText(childDetailResult.data.moWorkUnit);
            tv_phone.setText(childDetailResult.data.moTelephone);
            tv_roomdetail.setText(childDetailResult.data.houseProperty);
            tv_local.setText(childDetailResult.data.nativePlace);
            tv_nature.setText(childDetailResult.data.hukouType);

        }

    }

    private void getData() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.token, ""));
        j.put("id", id);

        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + Constant.GETCHILDINFO, j.toJSONString()
                , getClass().getName(), new HttpUtil.SuccessListener() {

            @Override
            public void setOnSuccessListener(String str) {

                if (CommonUtils.isNull(str)) {

                    showToast(Constant.NODATA);
                    return;
                }


                try {
                    ChildDetailResult childDetailResult = JSONObject.parseObject(str, ChildDetailResult.class);

                    if (childDetailResult.code == Constant.SUCCESS) {

                        setData(childDetailResult);

                    } else {

                        showToast(Constant.showErrorMsg(childDetailResult.code, ChildDetailActivity.this));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    showToast(Constant.NODATA);
                }
            }
        }, null);
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.family_detail));
        tv_child = (TextView) findViewById(R.id.tv_child);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        tv_birh = (TextView) findViewById(R.id.tv_birh);
        tv_pleacecode = (TextView) findViewById(R.id.tv_pleacecode);
        tv_dad = (TextView) findViewById(R.id.tv_dad);
        tv_job1 = (TextView) findViewById(R.id.tv_job1);
        tv_tel = (TextView) findViewById(R.id.tv_tel);
        tv_mom = (TextView) findViewById(R.id.tv_mom);
        tv_job2 = (TextView) findViewById(R.id.tv_job2);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_roomdetail = (TextView) findViewById(R.id.tv_roomdetail);
        tv_local = (TextView) findViewById(R.id.tv_local);
        tv_nature = (TextView) findViewById(R.id.tv_nature);
        iv_left.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }

    //    public void onResume() {
//        super.onResume();
//        MobclickAgent.onResume(this);       //统计时长
//    }
//
//    public void onPause() {
//        super.onPause();
//        MobclickAgent.onPause(this);
//    }
}
